package org.example.domain;

import org.optaplanner.core.api.domain.solution.PlanningEntityCollectionProperty;
import org.optaplanner.core.api.domain.solution.PlanningScore;
import org.optaplanner.core.api.domain.solution.PlanningSolution;
import org.optaplanner.core.api.domain.valuerange.ValueRangeProvider;
import org.optaplanner.core.api.score.buildin.hardsoft.HardSoftScore;

import java.util.ArrayList;
import java.util.List;

@PlanningSolution
public class CuttingPlanSolution {
    private List<RectangularPiece> stockPieces;
    private List<RectangularPiece> demandPieces;
    
    @PlanningEntityCollectionProperty
    private List<CuttingSpot> cuttingSpots;
    
    @PlanningScore
    private HardSoftScore score;

    public CuttingPlanSolution() {
        stockPieces = new ArrayList<>();
        demandPieces = new ArrayList<>();
        cuttingSpots = new ArrayList<>();
    }

    @ValueRangeProvider(id = "stockRange")
    public List<RectangularPiece> getStockPieces() {
        return stockPieces;
    }

    public void setStockPieces(List<RectangularPiece> stockPieces) {
        this.stockPieces = stockPieces;
    }

    public List<RectangularPiece> getDemandPieces() {
        return demandPieces;
    }

    public void setDemandPieces(List<RectangularPiece> demandPieces) {
        this.demandPieces = demandPieces;
    }

    public List<CuttingSpot> getCuttingSpots() {
        return cuttingSpots;
    }

    public void setCuttingSpots(List<CuttingSpot> cuttingSpots) {
        this.cuttingSpots = cuttingSpots;
    }

    public HardSoftScore getScore() {
        return score;
    }

    public void setScore(HardSoftScore score) {
        this.score = score;
    }

    @ValueRangeProvider(id = "xPositionRange")
    public List<Integer> getXPositionRange() {
        int maxWidth = stockPieces.stream()
                .mapToInt(RectangularPiece::getWidth)
                .max()
                .orElse(0);
        List<Integer> xRange = new ArrayList<>();
        for (int i = 0; i < maxWidth; i++) {
            xRange.add(i);
        }
        return xRange;
    }

    @ValueRangeProvider(id = "yPositionRange")
    public List<Integer> getYPositionRange() {
        int maxHeight = stockPieces.stream()
                .mapToInt(RectangularPiece::getHeight)
                .max()
                .orElse(0);
        List<Integer> yRange = new ArrayList<>();
        for (int i = 0; i < maxHeight; i++) {
            yRange.add(i);
        }
        return yRange;
    }
} 